package com.xiaoyi.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoyi.entity.SysMenu;
import com.xiaoyi.service.SysMenuService;
import com.xiaoyi.mapper.SysMenuMapper;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
* @author 小易
* @description 针对表【sys_menu】的数据库操作Service实现
* @createDate 2022-12-08 17:59:30
*/
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu>
    implements SysMenuService{

    @Override
    public List<SysMenu> buildTreeMenu(List<SysMenu> menuList) {
        List<SysMenu> resultMenuList = new ArrayList<>();
        for (SysMenu menu : menuList) {
            if (menu.getParentId() == 0L){
                resultMenuList.add(menu);
            }else {
                // 寻找子节点
                for (SysMenu sysMenu : menuList) {
                    if (Objects.equals(menu.getParentId(), sysMenu.getId())){
                        sysMenu.getChildren().add(menu);
                    }
                }
            }
        }
        return resultMenuList;
    }
}




